// pages/edit/edit.js
import nami from '../../nami/index';
import utils from '../../utils/utils';

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '',
    item: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var item = options.item;
    var that = this;
    var title = '添加' + new Date().format('yyyy-MM-dd') + '的日记';
    wx.setNavigationBarTitle({
      title: title
    });
    console.log(item);
    that.setData({
      item: item
    });
    wx.getStorage({
      key: 'diaryID',
      success: function (res) {
        console.log(res.data);
        wx.request({
          url: 'http://localhost:8080/request/diary/editDiary.groovy',
          data: {
            ID: res.data,
            item: item
          },
          success: function (res) {
            console.log('result:', res.data);
            if (res.data[item] != undefined && res.data[item] != null && res.data[item] != '') {
              that.setData({
                text: res.data[item]
              });
            }
          }
        });
      }
    });


  },

  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    var item = that.data.item;
    var content = e.detail.value[item];
    wx.getStorage({
      key: 'diaryID',
      success: function (storage) {
        wx.request({
          url: 'http://localhost:8080/request/diary/submit.groovy',
          data: {
            ID: storage.data,
            item: item,
            content: content
          },
          success: function (res) {
            console.log(res);
            if (res.data) {
              wx.navigateBack();
            } else {
              console.log('跳转失败');
            }
          }
        });
      }
    });
  },


})